<?php 
	namespace app\admin\controller;
	use think\Db;
	class Team extends Base{
		public function teamList(){			
//			$data = Db::name("service_team")->select();
//			$count = count($data);
			$data = Db::name('service_team')->order("team_id desc")->paginate(5,false, [ 'type'=>'Bootstrap2' ] );
			$this->assign("num",$data->getCurrentPage()*5-5);
			$this->assign("sl_data",$data);
			return $this->fetch();
		}
		public function addTeam(){
			if($_POST){
				$e = 0;//没上传错误
				$data = array(
					'team_title'=>$_POST['team_title'],
					'team_intro'=>$_POST['team_intro'],
					'leader_id'=>$_POST['leader_id']
				);
				
//				array(5) { ["name"]=> array(3) { [0]=> string(6) "a1.jpg" [1]=> string(6) "a2.jpg" [2]=> string(6) "a3.jpg" } 
//				["type"]=> array(3) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(10) "image/jpeg" } 
//				["tmp_name"]=> array(3) { [0]=> string(22) "C:\Windows\php4C64.tmp" [1]=> string(22) "C:\Windows\php4C65.tmp" [2]=> string(22) "C:\Windows\php4C66.tmp" } 
//				["error"]=> array(3) { [0]=> int(0) [1]=> int(0) [2]=> int(0) } ["size"]=> array(3) { [0]=> int(40994) [1]=> int(13416) [2]=> int(50346) } }
				$file = $_FILES['file'];
				if(count($file['name'])>0){
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
					for($i=0;$i<count($file['name']);$i++){
						$fileType = pathinfo($file['name'][$i],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确
							break;
						}
						if($file['size'][$i]>$size){
							$e = -2;//上传图片大于2M
							break;
						}
						if($file['error'][$i]!=0){
							$e = -3;//其他错误
							break;
						}
					}					
				}
				if($e==0){
						$files = array();
						for($i=0;$i<count($file['name']);$i++){
							$filename = time().rand(0, 1000).$file['name'][$i];
							$files[] = $filename;
							$info = move_uploaded_file($file['tmp_name'][$i], "./upload/admin/teampic/$filename");
						}
						$id = Db::name("service_team")->insert($data,false,true);
						for($i=0;$i<count($files);$i++){
							Db::name("teampic")->insert(['team_id'=>$id,"team_pic"=>$files[$i]]);
						}
						$this->redirect("teamlist");
					}
			}
			return $this->fetch();
		}
		public function getLeader($name){
			$data = Db::name("leader")->field("leader_name,leader_id")
			->where("leader_name", "like","%$name%")->limit(5)->select();
//			echo Db::name('leader')->getLastSql();die;
			$str = "";
			foreach($data as $leader){
				$str .= "<div id='".$leader['leader_id']."' class='lea_list'>".$leader['leader_name']."</div>";
			}
			echo $str;die;
		}
		public function editTeam($team_id){
			$data = Db::name('service_team as st')->field("st.*,le.leader_name,le.leader_id")->join("leader le","le.leader_id=st.leader_id")
			->where("st.team_id",$team_id)->find();
			$tid = $data['team_id'];
			$pics = Db::name("teampic")->where("team_id",$tid)->select();		
			$this->assign("teampic",$pics);
			$this->assign("team",$data);
			return $this->fetch();
		}
		public function saveEdit(){
			if($_POST){
				$e = 0;//没上传错误
				$tid = $_POST['team_id'];
				$data = array(
					'team_title'=>$_POST['team_title'],
					'team_intro'=>$_POST['team_intro'],
					'leader_id'=>$_POST['leader_id']
				);
				
//				array(5) { ["name"]=> array(3) { [0]=> string(6) "a1.jpg" [1]=> string(6) "a2.jpg" [2]=> string(6) "a3.jpg" } 
//				["type"]=> array(3) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(10) "image/jpeg" } 
//				["tmp_name"]=> array(3) { [0]=> string(22) "C:\Windows\php4C64.tmp" [1]=> string(22) "C:\Windows\php4C65.tmp" [2]=> string(22) "C:\Windows\php4C66.tmp" } 
//				["error"]=> array(3) { [0]=> int(0) [1]=> int(0) [2]=> int(0) } ["size"]=> array(3) { [0]=> int(40994) [1]=> int(13416) [2]=> int(50346) } }
				$file = $_FILES['file'];
				if(count($file['name'])>0){
					$tp = Db::name("teampic")->field("team_pic")->where("team_id",$tid)->select();
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
					for($i=0;$i<count($file['name']);$i++){
						$fileType = pathinfo($file['name'][$i],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确
							break;
						}
						if($file['size'][$i]>$size){
							$e = -2;//上传图片大于2M
							break;
						}
						if($file['error'][$i]!=0){
							$e = -3;//其他错误
							break;
						}
					}
					if($e==0)
						for($i=0;$i<count($tp);$i++){
							Db::name("teampic")->where("team_pic",$tp[$i]['team_pic'])->delete();
							if(file_exists("./upload/admin/teampic/".$tp[$i]['team_pic']))
								unlink("./upload/admin/teampic/".$tp[$i]['team_pic']);
						}
					
				}
				if($e==0){
						$files = array();
					for($i=0;$i<count($file['name']);$i++){
						$filename = time().rand(0, 1000).$file['name'][$i];
						$files[] = $filename;
						$info = move_uploaded_file($file['tmp_name'][$i], "./upload/admin/teampic/$filename");
					}
					$id = Db::name("service_team")->where("team_id",$tid)->update($data);
					for($i=0;$i<count($files);$i++){
						Db::name("teampic")->insert(['team_id'=>$tid,"team_pic"=>$files[$i]]);
					}
					$this->redirect("teamlist");
				}
			}
		}

		public function delTeam($team_id){
			Db::name("service_team")->where("team_id",$team_id)->delete();
			Db::name("teampic")->where("team_pic",$team_id)->delete();			
			echo "<script>alert('数据已删除')；</script>";
			$this->redirect("teamlist");
		}
	}
	
?>